﻿using HamcoWcs.Models.Common;
using HamcoWcs.Models.DeviceMsgs.SrmMsgs;
using HamcoWcs.Models.NodeInfos;
using System;
using System.Collections.Generic;

namespace HamcoWcs.Models.Devices
{
    public class SrmDto
    {
        public NodeInfoDto Aisle { get; set; }
        public string SrmName { get; set; }
        public string PlcName { get; set; }
        //public int DeviceMode { get; set; }
        //public int ErrorCode { get; set; }
        //public int IsTask { get; set; }
        //public int IsGoods { get; set; }
        //public string UnitId { get; set; } = "";
        //public int CurrentBay { get; set; }

        public int NoTaskTick { get; set; }
        public ushort DeviceMode { get; set; }
        public ushort ErrorCode { get; set; }
        public ushort IsTask { get; set; }
        public ushort IsGoods { get; set; }
        public string UnitId { get; set; } = "";
        public ushort CurrentBay { get; set; } = 1;
        //public ushort Enabled { get; set; }
        public ushort DestinationBay { get; set; }
        //public ushort LiftCount { get; set; } //20230627
        public ushort ServoState { get; set; }
        public UInt32 Speed { get; set; }
        public ushort BcrState { get; set; }
        public byte[] IOInfo { get; set; }
        public ushort Request { get; set; }

        public string GlobalPara { get; set; }
        
        public string MachineName { get; set; }
        public string PortName { get; set; }
        public int SendStartPoint { get; set; }
        public int RecvStartPoint { get; set; }

        private EnumMachineState _machineState = EnumMachineState.NONE;
        public EnumMachineState MachineState 
        {
            get => _machineState;
            set 
            {
                if (_machineState != value)
                    IsNeedReport = true;
                if (_machineState == EnumMachineState.DOWN && value == EnumMachineState.IDLE)
                {
                    _machineState = EnumMachineState.RUN;
                    return;
                }
                _machineState = value;
            }
        }
        public bool IsNeedReport{ get; set; } = false;
        public SrmTeachMsg TeachMsg;
        //public int Request { get; set; }
        public int Enabled
        {
            get => _enabled;
            set
            {
                bool isDiff = _enabled != value;
                if (isDiff)
                {
                    _enabled = value;
                    if (Aisle != null)
                        Aisle.CalcMode();
                }
            }
        }
        //public int DestinationBay { get; set; }

        private int _enabled = 1;

        public string LastMsg = string.Empty;

        public DateTime LastMsgTime = DateTime.Now;

        public SrmDto AnotherSrm { get; set; }

        public string DeviceName => $"{MachineName}-{PortName}";

        public int AlarmTick { get; set; }

        public List<int> BlockedBays { get; set; } = new List<int>();
        public bool IsCanDo()
        {
            //if (!ConnectionStatus) return false;
            if (Enabled == 0) return false;
            if (!(DeviceMode == 1 || DeviceMode == 6)) return false;
            if (IsTask == 1) return false;

            return true;
        }
        public bool IsRepeated(string msg)
        {
            var now = DateTime.Now;
            if (this.LastMsg == msg && (now - this.LastMsgTime).TotalSeconds <= GlobalValue.SRM_MSG_REPEAT_TIME)
                return true;
            this.LastMsg = msg;
            this.LastMsgTime = now;
            return false;
        }

        public void Update(SrmRecvMsg srmRecvMsg)
        {
            DeviceMode = srmRecvMsg.DeviceMode;
            ErrorCode = srmRecvMsg.ErrorCode;
            IsTask = srmRecvMsg.IsTask;
            IsGoods = srmRecvMsg.IsGoods;
            UnitId = srmRecvMsg.UnitId;
            CurrentBay = srmRecvMsg.CurrentBay;
            Enabled = srmRecvMsg.Enabled;
            DestinationBay = srmRecvMsg.DestinationBay;
            Request = srmRecvMsg.Request;

            Speed = srmRecvMsg.Speed;
            IOInfo = srmRecvMsg.IOInfo;
            BcrState = srmRecvMsg.BcrState;
            ServoState = srmRecvMsg.ServoState;
        }

    }
}
